﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using Chess;
using Draw;
using System.Net;
using System.Net.Sockets;
//using System.Threading;

namespace GameCoTuong
{
    public partial class frmMain : Form
    {
        #region Variable
        Presenter draw;

        int m_X1, m_Y1;
        int m_X2, m_Y2;

        Point startDragPoint = Point.Empty;
        Point chessPoint = Point.Empty;// Lưu lại vị trí ban đầu của con cờ khi chuột click nó
        Point _OriginPoint = Point.Empty;// Lưu lại vi trí củ của con cờ
        Point Rect = Point.Empty;

        ChessControl[] chessBlack = new ChessControl[16];//Quân cờ đen
        ChessControl[] chessRed = new ChessControl[16];//Quân cờ đỏ
        ChessColor me = ChessColor.Default;

        bool[] HasChess = new bool[90];//Mảng này cho biết vị trí nào đang có cờ
        ChessColor[] cColor = new ChessColor[90]; // Mảng này cho biết tại vị trí HasChess[index], cờ đó màu ji`. để biết mà ăn nó :D        

        string[] WB = new string[90];

        ChessColor player = ChessColor.Default;
        bool opposite = false;
        bool pausegame = false;
        //Biến kiểm tra xem có được phép ăn hay không
        bool eated = false;
        Size curSize;
        //
        
        #endregion

        #region Properties
        //Tra về số index của con cờ
        private int this[Point p]
        {
            get
            {                
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i] is ChessControl)
                    {
                        if (Controls[i].Location == p)
                            return ((ChessControl)Controls[i]).Index;
                    }
                }
                return -1;
            }
        }
        public bool Pause
        {
            get { return pausegame; }
            set { pausegame = value; }
        }
        #endregion
        
        #region Network
        Networks net;
        string mess = "";
        int p = 0;

        public ChessColor Player
        {
            get { return player; }
            set { player = value; }
        }
        #endregion Network

        public frmMain()
        {
            InitializeComponent();
            DoubleBuffered = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();
            Size = curSize = new Size(770, 615);            
            Load += new EventHandler(frmMain_Load);
            BackColor = Color.Khaki;
            net = new Networks(this);
            buttonStartServer.Click += new EventHandler(buttonStartServer_Click);
            buttonConnect.Click+=new EventHandler(buttonConnect_Click);

            buttonNewGame.Click += new EventHandler(buttonNewGame_Click);
            timerFlash.Tick += new EventHandler(timerFlash_Tick);
            timerFlash.Start();
            timerFlash.Interval = 100;
            //
           // buttonShowPanel.Click += new EventHandler(buttonShowPanel_Click);
            
            Resize += new EventHandler(frmMain_Resize);
            FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
            FormClosed += new FormClosedEventHandler(frmMain_FormClosed);
            //Move Form
            panelTop.MouseDown += new MouseEventHandler(Form_MouseDown);
            panelTop.MouseMove += new MouseEventHandler(Form_MouseMove);
            panelTop.MouseUp += new MouseEventHandler(Form_MouseUp);
            MouseDown += new MouseEventHandler(Form_MouseDown);
            MouseMove += new MouseEventHandler(Form_MouseMove);
            MouseUp += new MouseEventHandler(Form_MouseUp);
            panelRight.MouseDown += new MouseEventHandler(Form_MouseDown);
            panelRight.MouseMove += new MouseEventHandler(Form_MouseMove);
            panelRight.MouseUp += new MouseEventHandler(Form_MouseUp);            
            //End Move Form
            SetToolTip();          
        }
               
        #region Move From
        public int diff_x;
        public int diff_y;
        public bool mouse_down = false;
        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            mouse_down = false; 
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouse_down == true)
            {
                Point p = new Point(MousePosition.X - diff_x, MousePosition.Y - diff_y);
                Form.ActiveForm.Location = p;
            } 
        }

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            diff_x = Form.MousePosition.X - Form.ActiveForm.Location.X;
            diff_y = Form.MousePosition.Y - Form.ActiveForm.Location.Y;
            mouse_down = true;
        }
        #endregion
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Ban that su muon thoat Game???", "Thoat", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                if (net.Connection == StateConnection.Connected)
                {                                        
                    net.sendPacket("X");
                    net.CloseConnect();
                    Close();
                }
            }
        }
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            Size = curSize;
        }

      

        private void timerFlash_Tick(object sender, EventArgs e)
        {
            int i = DateTime.Now.Second;
            Color textColor = p == 1 ? textColor = Color.Black : textColor = Color.White;
            if (i % 2 == 0)
                labelGo.ForeColor = textColor;
            else
                labelGo.ForeColor = panelRight.BackColor;
            
        }

        private void buttonNewGame_Click(object sender, EventArgs e)
        {
            if (net.Connection == StateConnection.Connected)
            {
                NewGame();
                net.sendPacket("N");
                pausegame = false;
                Refresh();
            }
            else
            {
                MessageBox.Show("Ban chua ket noi!.\nHoac bi mat ket noi.!!\nXin hay ket noi lai!!!.:)");
            }
        }

        private void buttonStartServer_Click(object sender, EventArgs e)
        {
            if (net.Connection == StateConnection.Breaken)
            {                
                net.Listen();
                pausegame = false;
            }
        }
       
        private void buttonConnect_Click(object sender, EventArgs e)
        {            
            if (net.Connection == StateConnection.Connecting || net.Connection ==StateConnection.Breaken)
            {
                net.ConnectTo(textBoxIpAddress.Text);
                setEnable(false);
                pausegame = false;
            }            
        }
               
        public void setEnable(bool p)
        {
            buttonStartServer.Enabled = p;
            buttonNewGame.Enabled = p;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

            Graphics dc = this.CreateGraphics();
            dc.SmoothingMode = SmoothingMode.AntiAlias;
            draw = new Presenter(dc);
            DatQuanCo();

            Random rnd = new Random();
            p = rnd.Next(0, 2);
            if (p == 1)
            {
                player = ChessColor.Black;
            }
            else
            {
                player = ChessColor.Red;
            }
            
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            draw = new Presenter(e.Graphics);
            draw.DrawChessBoard();
           
            if (p == 1)
            {
                setLabel(1);
            }
            else
            {
                setLabel(2);
            }
            if (net.Connection == StateConnection.Connected)
                pausegame = false;
            else
                pausegame = true;
        }

        private void DatQuanCo()
        {          
            Region rg = draw.REGION;
            #region Quân cờ đen            
            chessBlack[0] = new ChessControl();
            chessBlack[0].Image = ChessBlackImage.Images[0];
            chessBlack[0].Location = new Point(270, 30);
            chessBlack[0].Size = new Size(40, 40);
            chessBlack[0].Region = rg;
            chessBlack[0].Tag = "2tuong_marked";
            chessBlack[0].Type = ChessType.Tuong;
            chessBlack[0].Color = ChessColor.Black;
            chessBlack[0].Index = 0;
            chessBlack[0].Parent = this;

            chessBlack[1] = new ChessControl();
            chessBlack[1].Image = ChessBlackImage.Images[3];
            chessBlack[1].Location = new Point(30, 30);
            chessBlack[1].Size = new Size(40, 40);
            chessBlack[1].Region = rg;
            chessBlack[1].Tag = "2xe_marked_1";
            chessBlack[1].Type = ChessType.Xe;
            chessBlack[1].Color = ChessColor.Black;
            chessBlack[1].Index = 1;
            chessBlack[1].Parent = this;

            chessBlack[2] = new ChessControl();
            chessBlack[2].Image = ChessBlackImage.Images[3];
            chessBlack[2].Location = new Point(510, 30);
            chessBlack[2].Size = new Size(40, 40);
            chessBlack[2].Region = rg;
            chessBlack[2].Tag = "2xe_marked_2";
            chessBlack[2].Type = ChessType.Xe;
            chessBlack[2].Color = ChessColor.Black;
            chessBlack[2].Index = 2;
            chessBlack[2].Parent = this;

            chessBlack[3] = new ChessControl();
            chessBlack[3].Image = ChessBlackImage.Images[5];
            chessBlack[3].Location = new Point(90, 30);
            chessBlack[3].Size = new Size(40, 40);
            chessBlack[3].Region = rg;
            chessBlack[3].Tag = "2ma_marked_1";
            chessBlack[3].Type = ChessType.Ma;
            chessBlack[3].Color = ChessColor.Black;
            chessBlack[3].Index = 3;                 
            chessBlack[3].Parent = this;

            chessBlack[4] = new ChessControl();
            chessBlack[4].Image = ChessBlackImage.Images[5];
            chessBlack[4].Location = new Point(450, 30);
            chessBlack[4].Size = new Size(40, 40);
            chessBlack[4].Region = rg;
            chessBlack[4].Tag = "2ma_marked_2";
            chessBlack[4].Type = ChessType.Ma;
            chessBlack[4].Color = ChessColor.Black;
            chessBlack[4].Index = 4;
            chessBlack[4].Parent = this;

            chessBlack[5] = new ChessControl();
            chessBlack[5].Image = ChessBlackImage.Images[2];
            chessBlack[5].Location = new Point(150, 30);
            chessBlack[5].Size = new Size(40, 40);
            chessBlack[5].Region = rg;
            chessBlack[5].Tag = "2tinh_marked_1";
            chessBlack[5].Type = ChessType.Tinh;
            chessBlack[5].Color = ChessColor.Black;
            chessBlack[5].Index = 5;
            chessBlack[5].Parent = this;

            chessBlack[6] = new ChessControl();
            chessBlack[6].Image = ChessBlackImage.Images[2];
            chessBlack[6].Location = new Point(390, 30);
            chessBlack[6].Size = new Size(40, 40);
            chessBlack[6].Region = rg;
            chessBlack[6].Tag = "2tinh_marked_2";
            chessBlack[6].Type = ChessType.Tinh;
            chessBlack[6].Color = ChessColor.Black;
            chessBlack[6].Index = 6;
            chessBlack[6].Parent = this;

            chessBlack[7] = new ChessControl();
            chessBlack[7].Image = ChessBlackImage.Images[1];
            chessBlack[7].Location = new Point(210, 30);
            chessBlack[7].Size = new Size(40, 40);
            chessBlack[7].Region = rg;
            chessBlack[7].Tag = "2sy_marked_1";
            chessBlack[7].Type = ChessType.Si;
            chessBlack[7].Color = ChessColor.Black;
            chessBlack[7].Index = 7;
            chessBlack[7].Parent = this;

            chessBlack[8] = new ChessControl();
            chessBlack[8].Image = ChessBlackImage.Images[1];
            chessBlack[8].Location = new Point(330, 30);
            chessBlack[8].Size = new Size(40, 40);
            chessBlack[8].Region = rg;
            chessBlack[8].Tag = "2sy_marked_2";
            chessBlack[8].Type = ChessType.Si;
            chessBlack[8].Color = ChessColor.Black;
            chessBlack[8].Index = 8;
            chessBlack[8].Parent = this;

            chessBlack[9] = new ChessControl();
            chessBlack[9].Image = ChessBlackImage.Images[4];
            chessBlack[9].Location = new Point(90, 150);
            chessBlack[9].Size = new Size(40, 40);
            chessBlack[9].Region = rg;
            chessBlack[9].Tag = "2phao_marked_1";
            chessBlack[9].Type = ChessType.Phao;
            chessBlack[9].Color = ChessColor.Black;
            chessBlack[9].Index = 9;
            chessBlack[9].Parent = this;

            chessBlack[10] = new ChessControl();
            chessBlack[10].Image = ChessBlackImage.Images[4];
            chessBlack[10].Location = new Point(450, 150);
            chessBlack[10].Size = new Size(40, 40);
            chessBlack[10].Region = rg;
            chessBlack[10].Tag = "2phao_marked_2";
            chessBlack[10].Type = ChessType.Phao;
            chessBlack[10].Color = ChessColor.Black;
            chessBlack[10].Index = 10;
            chessBlack[10].Parent = this;

            chessBlack[11] = new ChessControl();
            chessBlack[11].Image = ChessBlackImage.Images[6];
            chessBlack[11].Location = new Point(30, 210);
            chessBlack[11].Size = new Size(40, 40);
            chessBlack[11].Region = rg;
            chessBlack[11].Tag = "2chot_marked_1";
            chessBlack[11].Type = ChessType.Chot;
            chessBlack[11].Color = ChessColor.Black;
            chessBlack[11].Index = 11;
            chessBlack[11].Parent = this;

            chessBlack[12] = new ChessControl();
            chessBlack[12].Image = ChessBlackImage.Images[6];
            chessBlack[12].Location = new Point(150, 210);
            chessBlack[12].Size = new Size(40, 40);
            chessBlack[12].Region = rg;
            chessBlack[12].Tag = "2chot_marked_2";
            chessBlack[12].Type = ChessType.Chot;
            chessBlack[12].Color = ChessColor.Black;
            chessBlack[12].Index = 12;
            chessBlack[12].Parent = this;

            chessBlack[13] = new ChessControl();
            chessBlack[13].Image = ChessBlackImage.Images[6];
            chessBlack[13].Location = new Point(270, 210);
            chessBlack[13].Size = new Size(40, 40);
            chessBlack[13].Region = rg;
            chessBlack[13].Tag = "2chot_marked_3";
            chessBlack[13].Type = ChessType.Chot;
            chessBlack[13].Color = ChessColor.Black;
            chessBlack[13].Index = 13;
            chessBlack[13].Parent = this;

            chessBlack[14] = new ChessControl();
            chessBlack[14].Image = ChessBlackImage.Images[6];
            chessBlack[14].Location = new Point(390, 210);
            chessBlack[14].Size = new Size(40, 40);
            chessBlack[14].Region = rg;
            chessBlack[14].Tag = "2chot_marked_4";
            chessBlack[14].Type = ChessType.Chot;
            chessBlack[14].Color = ChessColor.Black;
            chessBlack[14].Index = 14;
            chessBlack[14].Parent = this;

            chessBlack[15] = new ChessControl();
            chessBlack[15].Image = ChessBlackImage.Images[6];
            chessBlack[15].Location = new Point(510, 210);
            chessBlack[15].Size = new Size(40, 40);
            chessBlack[15].Region = rg;
            chessBlack[15].Tag = "2chot_marked_5";
            chessBlack[15].Type = ChessType.Chot;
            chessBlack[15].Color = ChessColor.Black;
            chessBlack[15].Index = 15;
            chessBlack[15].Parent = this;
            #endregion

            #region Quân cờ do           
            chessRed[0] = new ChessControl();
            chessRed[0].Image = ChessRedImage.Images[0];
            chessRed[0].Location = new Point(270, 570);
            chessRed[0].Size = new Size(40, 40);
            chessRed[0].Region = rg;
            chessRed[0].Tag = "1tuong_marked";
            chessRed[0].Type = ChessType.Tuong;
            chessRed[0].Color = ChessColor.Red;
            chessRed[0].Index = 0;
            chessRed[0].Parent = this;

            chessRed[1] = new ChessControl();
            chessRed[1].Image = ChessRedImage.Images[3];
            chessRed[1].Location = new Point(30, 570);
            chessRed[1].Size = new Size(40, 40);
            chessRed[1].Region = rg;
            chessRed[1].Tag = "1xe_marked_1";
            chessRed[1].Type = ChessType.Xe;
            chessRed[1].Color = ChessColor.Red;
            chessRed[1].Index = 1;
            chessRed[1].Parent = this;

            chessRed[2] = new ChessControl();
            chessRed[2].Image = ChessRedImage.Images[3];
            chessRed[2].Location = new Point(510, 570);
            chessRed[2].Size = new Size(40, 40);
            chessRed[2].Region = rg;
            chessRed[2].Tag = "1xe_marked_2";
            chessRed[2].Type = ChessType.Xe;
            chessRed[2].Color = ChessColor.Red;
            chessRed[2].Index = 2;
            chessRed[2].Parent = this;

            chessRed[3] = new ChessControl();
            chessRed[3].Image = ChessRedImage.Images[5];
            chessRed[3].Location = new Point(90, 570);
            chessRed[3].Size = new Size(40, 40);
            chessRed[3].Region = rg;
            chessRed[3].Tag = "1ma_marked_1";
            chessRed[3].Type = ChessType.Ma;
            chessRed[3].Color = ChessColor.Red;
            chessRed[3].Index = 3;
            chessRed[3].Parent = this;

            chessRed[4] = new ChessControl();
            chessRed[4].Image = ChessRedImage.Images[5];
            chessRed[4].Location = new Point(450, 570);
            chessRed[4].Size = new Size(40, 40);
            chessRed[4].Region = rg;
            chessRed[4].Tag = "1ma_marked_2";
            chessRed[4].Type = ChessType.Ma;
            chessRed[4].Color = ChessColor.Red;
            chessRed[4].Index = 4;
            chessRed[4].Parent = this;

            chessRed[5] = new ChessControl();
            chessRed[5].Image = ChessRedImage.Images[2];
            chessRed[5].Location = new Point(150, 570);
            chessRed[5].Size = new Size(40, 40);
            chessRed[5].Region = rg;
            chessRed[5].Tag = "1tinh_marked_1";
            chessRed[5].Type = ChessType.Tinh;
            chessRed[5].Color = ChessColor.Red;
            chessRed[5].Index = 5;
            chessRed[5].Parent = this;

            chessRed[6] = new ChessControl();
            chessRed[6].Image = ChessRedImage.Images[2];
            chessRed[6].Location = new Point(390, 570);
            chessRed[6].Size = new Size(40, 40);
            chessRed[6].Region = rg;
            chessRed[6].Tag = "1tinh_marked_2";
            chessRed[6].Type = ChessType.Tinh;
            chessRed[6].Color = ChessColor.Red;
            chessRed[6].Index = 6;
            chessRed[6].Parent = this;

            chessRed[7] = new ChessControl();
            chessRed[7].Image = ChessRedImage.Images[1];
            chessRed[7].Location = new Point(210, 570);
            chessRed[7].Size = new Size(40, 40);
            chessRed[7].Region = rg;
            chessRed[7].Tag = "1sy_marked_1";
            chessRed[7].Type = ChessType.Si;
            chessRed[7].Color = ChessColor.Red;
            chessRed[7].Index = 7;
            chessRed[7].Parent = this;

            chessRed[8] = new ChessControl();
            chessRed[8].Image = ChessRedImage.Images[1];
            chessRed[8].Location = new Point(330, 570);
            chessRed[8].Size = new Size(40, 40);
            chessRed[8].Region = rg;
            chessRed[8].Tag = "1sy_marked_2";
            chessRed[8].Type = ChessType.Si;
            chessRed[8].Color = ChessColor.Red;
            chessRed[8].Index = 8;
            chessRed[8].Parent = this;

            chessRed[9] = new ChessControl();
            chessRed[9].Image = ChessRedImage.Images[4];
            chessRed[9].Location = new Point(90, 450);
            chessRed[9].Size = new Size(40, 40);
            chessRed[9].Region = rg;
            chessRed[9].Tag = "1phao_marked_1";
            chessRed[9].Type = ChessType.Phao;
            chessRed[9].Color = ChessColor.Red;
            chessRed[9].Index = 9;
            chessRed[9].Parent = this;

            chessRed[10] = new ChessControl();
            chessRed[10].Image = ChessRedImage.Images[4];
            chessRed[10].Location = new Point(450, 450);
            chessRed[10].Size = new Size(40, 40);
            chessRed[10].Region = rg;
            chessRed[10].Tag = "1phao_marked_2";
            chessRed[10].Type = ChessType.Phao;
            chessRed[10].Color = ChessColor.Red;
            chessRed[10].Index = 10;
            chessRed[10].Parent = this;

            chessRed[11] = new ChessControl();
            chessRed[11].Image = ChessRedImage.Images[6];
            chessRed[11].Location = new Point(30, 390);
            chessRed[11].Size = new Size(40, 40);
            chessRed[11].Region = rg;
            chessRed[11].Tag = "1chot_marked_1";
            chessRed[11].Type = ChessType.Chot;
            chessRed[11].Color = ChessColor.Red;
            chessRed[11].Index = 11;
            chessRed[11].Parent = this;

            chessRed[12] = new ChessControl();
            chessRed[12].Image = ChessRedImage.Images[6];
            chessRed[12].Location = new Point(150, 390);
            chessRed[12].Size = new Size(40, 40);
            chessRed[12].Region = rg;
            chessRed[12].Tag = "1chot_marked_2";
            chessRed[12].Type = ChessType.Chot;
            chessRed[12].Color = ChessColor.Red;
            chessRed[12].Index = 12;
            chessRed[12].Parent = this;

            chessRed[13] = new ChessControl();
            chessRed[13].Image = ChessRedImage.Images[6];
            chessRed[13].Location = new Point(270, 390);
            chessRed[13].Size = new Size(40, 40);
            chessRed[13].Region = rg;
            chessRed[13].Tag = "1chot_marked_3";
            chessRed[13].Type = ChessType.Chot;
            chessRed[13].Color = ChessColor.Red;
            chessRed[13].Index = 13;
            chessRed[13].Parent = this;

            chessRed[14] = new ChessControl();
            chessRed[14].Image = ChessRedImage.Images[6];
            chessRed[14].Location = new Point(390, 390);
            chessRed[14].Size = new Size(40, 40);
            chessRed[14].Region = rg;
            chessRed[14].Tag = "1chot_marked_4";
            chessRed[14].Type = ChessType.Chot;
            chessRed[14].Color = ChessColor.Red;
            chessRed[14].Index = 14;
            chessRed[14].Parent = this;

            chessRed[15] = new ChessControl();
            chessRed[15].Image = ChessRedImage.Images[6];
            chessRed[15].Location = new Point(510, 390);
            chessRed[15].Size = new Size(40, 40);
            chessRed[15].Region = rg;
            chessRed[15].Tag = "1chot_marked_5";
            chessRed[15].Type = ChessType.Chot;
            chessRed[15].Color = ChessColor.Red;
            chessRed[15].Index = 15;
            chessRed[15].Parent = this;
            #endregion

            #region Add event cho các quân cờ
            for (int i = 0; i < 16; i++)
            {
                chessRed[i].MouseDown += new MouseEventHandler(Chess_MouseDown);
                chessRed[i].MouseMove += new MouseEventHandler(Chess_MouseMove);
                chessRed[i].MouseUp += new MouseEventHandler(Chess_MouseUp);

                chessBlack[i].MouseDown += new MouseEventHandler(Chess_MouseDown);
                chessBlack[i].MouseMove += new MouseEventHandler(Chess_MouseMove);
                chessBlack[i].MouseUp += new MouseEventHandler(Chess_MouseUp);
            }
            #endregion
            
            #region Chổ nào có quân cờ, cho đó là True
            for (int i = 0; i < 90; i++)
            {
                HasChess[i] = false;
                cColor[i] = ChessColor.Default;
                WB[i] = "";
            }
            //Vị trí đặt quân cờ Đen
            HasChess[0] = true; cColor[0] = ChessColor.Black; WB[0] = "2xe_marked_1";
            HasChess[1] = true; cColor[1] = ChessColor.Black; WB[1] = "2ma_marked_1";
            HasChess[2] = true; cColor[2] = ChessColor.Black; WB[2] = "2tinh_marked_1";
            HasChess[3] = true; cColor[3] = ChessColor.Black; WB[3] = "2sy_marked_1";
            HasChess[4] = true; cColor[4] = ChessColor.Black; WB[4] = "2tuong_marked";
            HasChess[5] = true; cColor[5] = ChessColor.Black; WB[5] = "2sy_marked_2";
            HasChess[6] = true; cColor[6] = ChessColor.Black; WB[6] = "2tinh_marked_2";
            HasChess[7] = true; cColor[7] = ChessColor.Black; WB[7] = "2ma_marked_2";
            HasChess[8] = true; cColor[8] = ChessColor.Black; WB[8] = "2xe_marked_2";
            HasChess[19] = true; cColor[19] = ChessColor.Black; WB[19] = "2phao_marked_1";
            HasChess[25] = true; cColor[25] = ChessColor.Black; WB[25] = "2phao_marked_2";
            HasChess[27] = true; cColor[27] = ChessColor.Black; WB[27] = "2chot_marked_1";
            HasChess[29] = true; cColor[29] = ChessColor.Black; WB[29] = "2chot_marked_2";
            HasChess[31] = true; cColor[31] = ChessColor.Black; WB[31] = "2chot_marked_3";
            HasChess[33] = true; cColor[33] = ChessColor.Black; WB[33] = "2chot_marked_4";
            HasChess[35] = true; cColor[35] = ChessColor.Black; WB[35] = "2chot_marked_5";
            //Vị trí đặt quân cờ Trắng
            HasChess[54] = true; cColor[54] = ChessColor.Red; WB[54] = "1chot_marked_1";
            HasChess[56] = true; cColor[56] = ChessColor.Red; WB[56] = "1chot_marked_2";
            HasChess[58] = true; cColor[58] = ChessColor.Red; WB[58] = "1chot_marked_3";
            HasChess[60] = true; cColor[60] = ChessColor.Red; WB[60] = "1chot_marked_4";
            HasChess[62] = true; cColor[62] = ChessColor.Red; WB[62] = "1chot_marked_5";
            HasChess[64] = true; cColor[64] = ChessColor.Red; WB[64] = "1hao_marked_1";
            HasChess[70] = true; cColor[70] = ChessColor.Red; WB[70] = "1phao_marked_2";
            HasChess[81] = true; cColor[81] = ChessColor.Red; WB[81] = "1xe_marked_1";
            HasChess[82] = true; cColor[82] = ChessColor.Red; WB[82] = "1ma_marked_1";
            HasChess[83] = true; cColor[83] = ChessColor.Red; WB[83] = "1tinh_marked_1";
            HasChess[84] = true; cColor[84] = ChessColor.Red; WB[84] = "1sy_marked_1";
            HasChess[85] = true; cColor[85] = ChessColor.Red; WB[85] = "1tuong_marked";
            HasChess[86] = true; cColor[86] = ChessColor.Red; WB[86] = "1sy_marked_2";
            HasChess[87] = true; cColor[87] = ChessColor.Red; WB[87] = "1tinh_marked_2";
            HasChess[88] = true; cColor[88] = ChessColor.Red; WB[88] = "1ma_marked_2";
            HasChess[89] = true; cColor[89] = ChessColor.Red; WB[89] = "1xe_marked_2";
            #endregion

            
        }
        
        private void Chess_MouseDown(object sender, MouseEventArgs e)
        {            
            if (e.Button == MouseButtons.Left)
            {
                ChessControl ch = (ChessControl)sender;
                ch.BringToFront();
                startDragPoint = e.Location;
                chessPoint = ch.Location;
                _OriginPoint = new Point(chessPoint.X + 20, chessPoint.Y + 20);

                //this.Cursor = Cursors.Hand;
            }
            else
                return;
        }

        private void Chess_MouseMove(object sender, MouseEventArgs e)
        {
            ChessControl ch = (ChessControl)sender;
            if (e.Button == MouseButtons.Left && !pausegame)
            {
                ch.Location = new Point(ch.Left + e.X - startDragPoint.X, ch.Top + e.Y - startDragPoint.Y);
            }
            else
                return;
        }
        
        private void Chess_MouseUp(object sender, MouseEventArgs e)
        {            
            //
            if (e.Button == MouseButtons.Right)
                return;
            ChessControl cControl = (ChessControl)sender;

            //Tranh truong hop chua ket noi ma choi.
            if (net.Connection ==StateConnection.Breaken || player == ChessColor.Default)
            {
                cControl.Location = chessPoint;
                return;
            }
            
            m_X1 = cControl.Location.X;
            m_Y1 = cControl.Location.Y;
            draw.SnapToPoint(ref m_X1, ref m_Y1);
            m_X2 = m_X1;
            m_Y2 = m_Y1;

            Point newpoint = new Point(m_X2, m_Y2);
            //Nếu Point cu~ == Point mới ==> ko làm gì hết.
            if (newpoint == _OriginPoint || draw.CheckPoint(newpoint)==false)
            {
                cControl.Location = chessPoint;
                return;
            }

            
            //2 index này là tính trên cả bàn cờ 0->89
            int indexNew = draw.MappingPointToIndex(newpoint);
            int indexOld = draw.MappingPointToIndex(_OriginPoint);

            //index này thì trả về index của ChessControl tương ứng với 2 mảng chessBlack & chessWhite 0->15
            int nIndex = draw.MappingPointToIndex(newpoint);
            int cIndex = -1;
                      
            if (draw.CheckPoint(newpoint))
                cIndex = this[new Point(newpoint.X - 20, newpoint.Y - 20)];

            #region Khi 2 Tướng đối mặt nhau
            int bGIndex = cControl.Color == ChessColor.Black ? indexNew : (int)GeneralInfo("2tuong_marked", Info.Index);
            int wGIndex = cControl.Color == ChessColor.Red ? indexNew : (int)GeneralInfo("1tuong_marked", Info.Index);
            #endregion
            //
            opposite = GameAI.GeneralOpposition(HasChess, bGIndex, wGIndex);
            
            Checked move = CheckMoveValid(cControl.Index, indexNew, indexOld, nIndex, newpoint, _OriginPoint, (int)(cControl.Color));
            if (move != Checked.MoveAndEated)
            {
                cControl.Location = chessPoint;
                return;
            }
            else
            {
                switch (player)
                {
                    case ChessColor.Black://Black
                        {
                            if (cControl.Type == ChessType.Tuong)
                                if (opposite)
                                {
                                    MessageBox.Show("Doi mat tuong.\nBan khong the di con nay...", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    cControl.Location = chessPoint;
                                    return;
                                }
                            ///
                            player = ChessColor.Default;
                            int color = (int)cControl.Color;
                            Process(cControl.Index, indexNew, indexOld, nIndex, newpoint, _OriginPoint, color);
                            int ct = Convert.ToInt32(KiemTraChieuTuong(cControl.Color));// MessageBox.Show("Chiếu tướng nè");
                            ///
                            #region Khi 2 Tướng đối mặt nhau
                            int bGeneralIndex = (int)GeneralInfo("2tuong_marked", Info.Index);
                            int wGeneralIndex = (int)GeneralInfo("1tuong_marked", Info.Index);
                            #endregion
                            //
                            if (GameAI.GeneralOpposition(HasChess, bGeneralIndex, wGeneralIndex))
                            {
                                MessageBox.Show("Doi mat tuong.\nBan khong the di con nay...", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                //
                                cControl.Location = chessPoint;
                                HasChess[indexNew] = false;
                                HasChess[indexOld] = true;
                                cColor[indexNew] = ChessColor.Default;
                                cColor[indexOld] = cControl.Color;
                                WB[indexOld] = cControl.Tag.ToString();
                                WB[indexNew] = "";
                                //
                               
                                player = ChessColor.Black;
                                return;
                            }
                            else
                            {
                                
                                net.sendPacket(indexNew + "," + indexOld + "," + nIndex + ",1" + "," + cIndex + "," + newpoint.X + "," + newpoint.Y + "," + _OriginPoint.X + "," + _OriginPoint.Y + "," + cControl.Index + "," + ct + "," + color);
                                
                            }
                            if (me == ChessColor.Default)
                                me = ChessColor.Black;

                        } break;
                    case ChessColor.Red://White
                        {
                            if (cControl.Type == ChessType.Tuong)
                                if (opposite)
                                {
                                    MessageBox.Show("Doi mat tuong.\nBan khong the di con nay...", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    cControl.Location = chessPoint;
                                    return;
                                }
                            ///
                            player = ChessColor.Default;
                            int color = (int)cControl.Color;
                            Process(cControl.Index, indexNew, indexOld, nIndex, newpoint, _OriginPoint, color);
                            int ct = Convert.ToInt32(KiemTraChieuTuong(cControl.Color));// MessageBox.Show("Chiếu tướng nè");
                            ///
                            //
                            #region Khi 2 Tướng đối mặt nhau
                            int bGeneralIndex = (int)GeneralInfo("2tuong_marked", Info.Index);
                            int wGeneralIndex = (int)GeneralInfo("1tuong_marked", Info.Index);
                            #endregion
                            //
                            if (GameAI.GeneralOpposition(HasChess, bGeneralIndex, wGeneralIndex))
                            {
                                MessageBox.Show("Doi mat tuong.\nBan khong the di con nay...", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                //
                                cControl.Location = chessPoint;
                                HasChess[indexNew] = false;
                                HasChess[indexOld] = true;
                                cColor[indexNew] = ChessColor.Default;
                                cColor[indexOld] = cControl.Color;
                                WB[indexOld] = cControl.Tag.ToString();
                                WB[indexNew] = "";
                                //
                               
                                player = ChessColor.Red;
                                return;
                            }
                            else
                            {               
                                
                                net.sendPacket(indexNew + "," + indexOld + "," + nIndex + ",2" + "," + cIndex + "," + newpoint.X + "," + newpoint.Y + "," + _OriginPoint.X + "," + _OriginPoint.Y + "," + cControl.Index + "," + ct + "," + color);
                                
                            }
                            if (me == ChessColor.Default)
                                me = ChessColor.Red;
                        } break;
                    default:
                        cControl.Location = chessPoint;
                        break;
                }
            }
            if (net.Connection == StateConnection.Connected)
            {
                if ((int)GeneralInfo("2tuong_marked", Info.Index) == -1)
                {
                    net.sendPacket("W_WIN");
                    MessageBox.Show("Chuc mung!!!. Ban da thang.\n Nhan NewGame de choi lai...");
                    pausegame = true;
                    return;
                }
                if ((int)GeneralInfo("1tuong_marked", Info.Index) == -1)
                {
                    net.sendPacket("B_WIN");
                    MessageBox.Show("Chuc mung!!!. Ban da thang.\n Nhan NewGame de choi lai...");
                    pausegame = true;
                    return;
                }
            }
            this.Cursor = Cursors.Default;
                        
        }
        
        private delegate void ProcessDelegate(int cControlIndex, int indexNew, int indexOld, int nIndex, Point newpoint, Point OriginPoint, int color);        
        public void Process(int cControlIndex, int indexNew, int indexOld, int nIndex, Point newpoint, Point OriginPoint, int color)
        {

            if (InvokeRequired)
            {
                Invoke(new ProcessDelegate(Process), cControlIndex, indexNew, indexOld, nIndex, newpoint, OriginPoint, color);
                return;
            }



            ChessControl cControl = new ChessControl();
            if (color == 1)
                cControl = chessBlack[cControlIndex];
            else if (color == 2)
                cControl = chessRed[cControlIndex];
            else
                return;                   

            #region if process
            if (draw.CheckPoint(newpoint))
            {
                switch (cControl.Type)
                {
                    case ChessType.Tuong://Tướng
                        {
                            Pawns general = new Tuong(newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                            bool ok = general.CheckIndex();
                            eated = general.CheckEated(cColor[nIndex]);
                            if (ok)
                            {
                                if (HasChess[indexNew] == false)
                                {
                                    this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                }
                                else if (HasChess[indexNew] == true)
                                {
                                    if (eated)
                                    {
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                    }
                                    else
                                        cControl.Location = chessPoint;
                                }
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    case ChessType.Si://Sĩ
                        {
                            Pawns advisor = new Sy(indexNew, indexOld, cControl.Color);
                            bool ok = advisor.CheckIndex();
                            eated = advisor.CheckEated(cColor[nIndex]);
                            if (ok)
                            {
                                if (HasChess[indexNew] == false)
                                {
                                    this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                }
                                else if (HasChess[indexNew] == true)
                                {
                                    if (eated)
                                    {
                                        //this.Controls.Remove(cControl.Color == ChessColor.Black ? chessWhite[this[new Point(newpoint.X - 20, newpoint.Y - 20)]] : chessBlack[this[new Point(newpoint.X - 20, newpoint.Y - 20)]]);
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                        //if (isRemoved)                                                                                 
                                    }
                                    else
                                        cControl.Location = chessPoint;
                                }
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    case ChessType.Tinh://Tượng.........................
                        {
                            Pawns elephant = new Tinh(HasChess, indexNew, indexOld, cControl.Color);
                            bool ok = elephant.CheckIndex();
                            eated = elephant.CheckEated(cColor[nIndex]);
                            if (ok)
                            {
                                if (HasChess[indexNew] == false)
                                {
                                    this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                }
                                else if (HasChess[indexNew] == true)
                                {
                                    if (eated)
                                    {
                                        //this.Controls.Remove(cControl.Color == ChessColor.Black ? chessWhite[this[new Point(newpoint.X - 20, newpoint.Y - 20)]] : chessBlack[this[new Point(newpoint.X - 20, newpoint.Y - 20)]]);
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                        //if(isRemoved)                                            
                                    }
                                    else
                                        cControl.Location = chessPoint;
                                }
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    case ChessType.Xe://Xe
                        {
                            Pawns chariot = new Xe(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                            bool ok = chariot.CheckPoint();
                            bool ok2 = chariot.CheckIndex();
                            eated = chariot.CheckEated(cColor[nIndex]);
                            if (ok)
                            {
                                if (ok2)
                                {
                                    if (HasChess[indexNew] == false)
                                    {
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                    }
                                    else if (HasChess[indexNew] == true)
                                    {
                                        if (eated)
                                        {
                                            //this.Controls.Remove(cControl.Color == ChessColor.Black ? chessWhite[this[new Point(newpoint.X - 20, newpoint.Y - 20)]] : chessBlack[this[new Point(newpoint.X - 20, newpoint.Y - 20)]]);
                                            this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                        }
                                        else
                                            cControl.Location = chessPoint;
                                    }
                                }
                                else
                                    cControl.Location = chessPoint;
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    case ChessType.Phao://Pháo
                        {
                            Pawns cannon = new Phao(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                            bool ok = cannon.CheckIndex();
                            bool ok2 = cannon.CheckPoint();
                            eated = cannon.CheckEated(cColor[nIndex]);
                            if (ok2)
                            {
                                if (HasChess[indexNew] == false)
                                {
                                    if (ok)
                                    {
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                    }
                                    else
                                        cControl.Location = chessPoint;
                                }
                                else if (HasChess[indexNew] == true)
                                {
                                    if (eated)
                                    {
                                        //this.Controls.Remove(cControl.Color == ChessColor.Black ? chessWhite[this[new Point(newpoint.X - 20, newpoint.Y - 20)]] : chessBlack[this[new Point(newpoint.X - 20, newpoint.Y - 20)]]);
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                    }
                                    else
                                        cControl.Location = chessPoint;
                                }
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    case ChessType.Ma://Mã
                        {
                            Pawns horse = new Ma(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                            bool ok = horse.CheckIndex();
                            bool ok2 = horse.CheckPoint();
                            eated = horse.CheckEated(cColor[nIndex]);

                            if (ok && ok2)
                            {
                                if (HasChess[indexNew] == false)
                                {
                                    this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                }
                                else if (HasChess[indexNew] == true)
                                {
                                    if (eated)
                                    {
                                        //this.Controls.Remove(cControl.Color == ChessColor.Black ? chessWhite[this[new Point(newpoint.X - 20, newpoint.Y - 20)]] : chessBlack[this[new Point(newpoint.X - 20, newpoint.Y - 20)]]);
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                    }
                                    else
                                        cControl.Location = chessPoint;
                                }
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    case ChessType.Chot://Chốt
                        {
                            Pawns soldier = new Tot(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                            bool ok2 = soldier.CheckPoint();
                            eated = soldier.CheckEated(cColor[nIndex]);
                            if (ok2)//Nếu vị trí đi là OK...
                            {
                                if (HasChess[indexNew] == false)//...và nếu vị trí mới CHƯA CÓ quân cờ nào được đặt
                                {
                                    this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, false);
                                }
                                else if (HasChess[indexNew] == true)//...và nếu vị trí mới ĐÃ CÓ quân cờ nào được đặt...
                                {
                                    if (eated)//...Nếu vị trí mới có quân cờ và khác màu thì ĂN NÓ
                                    {
                                        //this.Controls.Remove(cControl.Color == ChessColor.Black ? chessWhite[this[new Point(newpoint.X - 20, newpoint.Y - 20)]] : chessBlack[this[new Point(newpoint.X - 20, newpoint.Y - 20)]]);
                                        this.MoveChess(cControl, new Point(newpoint.X - 20, newpoint.Y - 20), indexNew, indexOld, true);
                                    }
                                    else//...Còn nếu cùng màu thì quay lại vị trí cũ
                                        cControl.Location = chessPoint;
                                }
                            }
                            else
                                cControl.Location = chessPoint;
                        }
                        break;
                    default:
                        break;
                }

            }
            else
                cControl.Location = chessPoint;
            #endregion
        }

        //Duy chuyển cờ đến vị trí mới
        private delegate void MoveChessDelegate(ChessControl cControl, Point newpoint, int indexNew, int indexOld, bool eated);
        private void MoveChess(ChessControl cControl, Point newpoint, int indexNew, int indexOld, bool eated)
        {              
            ///
            if (InvokeRequired)
            {
                Invoke(new MoveChessDelegate(MoveChess), cControl, newpoint, indexNew, indexOld, eated);
                return;
            }
            //#region Khi 2 Tướng đối mặt nhau
            //int bGeneralIndex = cControl.Color == ChessColor.Black ? indexNew : (int)GeneralInfo("General_B", Info.Index);
            //int wGeneralIndex = cControl.Color == ChessColor.White ? indexNew : (int)GeneralInfo("General_W", Info.Index);
            //#endregion
            ////
            //bool op = GameAI.GeneralOpposition(HasChess, bGeneralIndex, wGeneralIndex);
            ///
            if (eated)
            {                
                //if (op == false)                                                   

                this.Controls.Remove(cControl.Color == ChessColor.Black ? chessRed[this[new Point(newpoint.X, newpoint.Y)]] : chessBlack[this[new Point(newpoint.X, newpoint.Y)]]);
                //else
                //{
                //    cControl.Location = chessPoint;
                //    return;
                //}
            }
            cControl.Location = newpoint;
            HasChess[indexNew] = true;
            HasChess[indexOld] = false;
            cColor[indexNew] = cControl.Color;            
            cColor[indexOld] = ChessColor.Default;
            WB[indexOld] = "";
            WB[indexNew] = cControl.Tag.ToString();

            this.Refresh();
            draw.DrawRect(this.CreateGraphics(), new Point(newpoint.X + 20, newpoint.Y + 20));
            return;
        }        

        //Lấy chỉ số index của 2 con tướng
        private object GeneralInfo(string name,Info info)
        {
            switch (info)
            {
                case Info.Index://Tra ve Index
                    {
                        switch (name)
                        {
                            case "2tuong_marked":
                                {
                                    for (int i = 0; i < 90; i++)
                                        if (WB[i] == name)
                                            return i;
                                } break;
                            case "1tuong_marked":
                                {
                                    for (int i = 0; i < 90; i++)
                                        if (WB[i] == name)
                                            return i;
                                } break;
                            default: break;
                        }
                    } break;
                case Info.Point://Tra ve Vi tri
                    {
                        switch (name)
                        {
                            case "2tuong_marked":
                                {
                                    for (int i = 0; i < Controls.Count; i++)
                                    {
                                        if (Controls[i] is ChessControl)
                                        {
                                            if (((ChessControl)Controls[i]).Tag.ToString() == "2tuong_marked")
                                            {
                                                return ((ChessControl)Controls[i]).Location;
                                            }
                                        }
                                    }
                                } break;
                            case "1tuong_marked":
                                {
                                    for (int i = 0; i < Controls.Count; i++)
                                    {
                                        if (Controls[i] is ChessControl)
                                        {
                                            if (((ChessControl)Controls[i]).Tag.ToString() == "1tuong_marked")
                                            {
                                                return ((ChessControl)Controls[i]).Location;
                                            }
                                        }
                                    }
                                } break;
                            default: break;
                        }
                    } break;
            }
         
            return -1;
        }

        //Khi mình đi 1 con cờ, thì kiểm tra xem có chiếu tướng đối phương không ???
        private bool KiemTraChieuTuong(ChessColor color)
        {
            #region Khi 2 Tướng đối mặt nhau
            int bGeneralIndex = (int)GeneralInfo("2tuong_marked", Info.Index);
            int wGeneralIndex = (int)GeneralInfo("1tuong_marked", Info.Index);

            if (GameAI.GeneralOpposition(HasChess, bGeneralIndex, wGeneralIndex))
                return true;                   

            #endregion
            
            Point bGeneralPoint;
            Point wGeneralPoint;
            try
            {
                bGeneralPoint = (Point)GeneralInfo("2tuong_marked", Info.Point);
                wGeneralPoint = (Point)GeneralInfo("1tuong_marked", Info.Point);
            }
            catch
            {
                return false;
            }

            bool checkmate = false;
            switch (color)
            {
                case ChessColor.Black:
                    {
                        for (int i = 0; i < Controls.Count; i++)
                        {
                            if (Controls[i] is ChessControl)
                            {
                                if (((ChessControl)Controls[i]).Color == color /*&& ((ChessControl)Controls[i]).Type != ChessType.General*/)
                                {                                    
                                    checkmate = GameAI.CheckMate(((ChessControl)Controls[i]).Type, ((ChessControl)Controls[i]).Color, wGeneralPoint, ((ChessControl)Controls[i]).Location, wGeneralIndex, draw.MappingPointToIndex(new Point(((ChessControl)Controls[i]).Location.X + 20, ((ChessControl)Controls[i]).Location.Y + 20)));
                                }
                            }
                            if (checkmate)
                                return checkmate;
                        }
                    } break;
                case ChessColor.Red:
                    {
                        for (int i = 0; i < Controls.Count; i++)
                        {
                            if (Controls[i] is ChessControl)
                            {
                                if (((ChessControl)Controls[i]).Color == color /*&& ((ChessControl)Controls[i]).Type != ChessType.General*/)
                                {
                                    checkmate = GameAI.CheckMate(((ChessControl)Controls[i]).Type, ((ChessControl)Controls[i]).Color, bGeneralPoint, ((ChessControl)Controls[i]).Location, bGeneralIndex, draw.MappingPointToIndex(new Point(((ChessControl)Controls[i]).Location.X + 20, ((ChessControl)Controls[i]).Location.Y + 20)));
                                }
                            }
                            if (checkmate)
                                return checkmate;
                        }
                    } break;
                default:
                    break;
            }
            return false;
        }

        //Khi mình đi 1 con cờ, thì kiểm tra xem mình có đang bị chiếu tướng không ???. hàm này chuyền thông số ngược với hàm trên 
        private bool KiemTraBiChieuTuong(ChessColor color)
        {
            #region Khi 2 Tướng đối mặt nhau
            int bGeneralIndex = (int)GeneralInfo("2tuong_marked", Info.Index);
            int wGeneralIndex = (int)GeneralInfo("1tuong_marked", Info.Index);

            if (GameAI.GeneralOpposition(HasChess, bGeneralIndex, wGeneralIndex))
                return true;

            #endregion

            Point bGeneralPoint;
            Point wGeneralPoint;
            try
            {
                bGeneralPoint = (Point)GeneralInfo("2tuong_marked", Info.Point);
                wGeneralPoint = (Point)GeneralInfo("1tuong_marked", Info.Point);
            }
            catch
            {
                return false;
            }

            bool checkmate = false;
            switch (color)
            {
                case ChessColor.Black:
                    {
                        for (int i = 0; i < Controls.Count; i++)
                        {
                            if (Controls[i] is ChessControl)
                            {
                                if (((ChessControl)Controls[i]).Color != color /*&& ((ChessControl)Controls[i]).Type != ChessType.General*/)
                                {
                                    if (bGeneralPoint.X == ((ChessControl)Controls[i]).Location.X || bGeneralPoint.Y == ((ChessControl)Controls[i]).Location.Y)
                                        checkmate = GameAI.CheckMate(((ChessControl)Controls[i]).Type, ((ChessControl)Controls[i]).Color, bGeneralPoint, ((ChessControl)Controls[i]).Location, bGeneralIndex, draw.MappingPointToIndex(new Point(((ChessControl)Controls[i]).Location.X + 20, ((ChessControl)Controls[i]).Location.Y + 20)));
                                }
                            }
                            if (checkmate)
                                return checkmate;
                        }
                    } break;
                case ChessColor.Red:
                    {
                        for (int i = 0; i < Controls.Count; i++)
                        {
                            if (Controls[i] is ChessControl)
                            {
                                if (((ChessControl)Controls[i]).Color != color /*&& ((ChessControl)Controls[i]).Type != ChessType.General*/)
                                {
                                    if (wGeneralPoint.X == ((ChessControl)Controls[i]).Location.X || wGeneralPoint.Y == ((ChessControl)Controls[i]).Location.Y)
                                        checkmate = GameAI.CheckMate(((ChessControl)Controls[i]).Type, ((ChessControl)Controls[i]).Color, wGeneralPoint, ((ChessControl)Controls[i]).Location, wGeneralIndex, draw.MappingPointToIndex(new Point(((ChessControl)Controls[i]).Location.X + 20, ((ChessControl)Controls[i]).Location.Y + 20)));
                                }
                            }
                            if (checkmate)
                                return checkmate;
                        }
                    } break;
                default:
                    break;
            }
            return false;
        }

        private delegate void NewGameDelegate();
        public void NewGame()
        {
            pausegame = false;
            if (InvokeRequired)
            {
                Invoke(new NewGameDelegate(NewGame));
                return;
            }

            for (int i = 0; i < 16; i++)
            {
                Controls.Remove(chessRed[i]);
                Controls.Remove(chessBlack[i]);
            }

            Graphics dc = this.CreateGraphics();
            dc.SmoothingMode = SmoothingMode.AntiAlias;
            draw = new Presenter(dc);
            DatQuanCo();

            Random rnd = new Random();
            p = rnd.Next(0, 2);
            if (p == 1)
            {
                player = ChessColor.Black;
            }
            else
            {
                player = ChessColor.Red;                
            }           
                       
        }
        
        public override void Refresh()
        {
            base.Refresh();
        }

        private delegate void SetStatusMessageDelegate(string strArg);
        public void SetStatusMessage(string mess)
        {
            
            this.mess = mess;
            if (InvokeRequired)
            {
                Invoke(new SetStatusMessageDelegate(SetStatusMessage), new object[] { mess });
                return;
            }

            lblStatus.Text = mess;
            
        }
        
        private void setLabel(int p)
        {
            if (p == 1)
            {
                labelGo.Text = "Den di truoc.";
            }
            else
            {
                labelGo.Text = "Do di truoc.";
            }
        }
        
       

        private Checked CheckMoveValid(int cControlIndex, int indexNew, int indexOld, int nIndex, Point newpoint, Point OriginPoint, int color)
        {
            ChessControl cControl = new ChessControl();
            if (color == 1)
                cControl = chessBlack[cControlIndex];
            else if (color == 2)
                cControl = chessRed[cControlIndex];
            else
                return Checked.Default;           

            bool eated = false;
            bool m = false;
            switch (cControl.Type)
            {
                case ChessType.Tuong:
                    {
                        Pawns general = new Tuong(newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                        eated = general.CheckEated(cColor[nIndex]);                        

                        m = general.CheckIndex();
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                    }; //break;
                case ChessType.Si:
                    {
                        Pawns advisor = new Sy(indexNew, indexOld, cControl.Color);
                        eated = advisor.CheckEated(cColor[nIndex]);
                        m = advisor.CheckIndex();
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                    }; //break;
                case ChessType.Tinh:
                    {
                        Pawns elephant = new Tinh(HasChess, indexNew, indexOld, cControl.Color);
                        eated = elephant.CheckEated(cColor[nIndex]);
                        m = elephant.CheckIndex();
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                    } //break;
                case ChessType.Xe:
                    {
                        Pawns chariot = new Xe(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                        m = (chariot.CheckPoint() && chariot.CheckIndex());
                        eated = chariot.CheckEated(cColor[nIndex]);
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                    }; //break;
                case ChessType.Phao:
                    {
                        Pawns cannon = new Phao(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                        m = (/*cannon.CheckIndex() &&*/ cannon.CheckPoint());
                        eated = cannon.CheckEated(cColor[nIndex]);
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                        
                    }; //break;
                case ChessType.Ma:
                    {
                        Pawns horse = new Ma(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                        m = (horse.CheckIndex() && horse.CheckPoint());
                        eated = horse.CheckEated(cColor[nIndex]);
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                    } //break;
                case ChessType.Chot:
                    {
                        Pawns soldier = new Tot(HasChess, newpoint, OriginPoint, indexNew, indexOld, cControl.Color);
                        m = soldier.CheckPoint();
                        eated = soldier.CheckEated(cColor[nIndex]);
                        if ((m == true && eated == true) || (m == true && eated == false))
                            return Checked.MoveAndEated;
                        else
                            return Checked.NoMoveable;
                    } //break;
            }
            return Checked.Default;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            timerFlash.Stop();           
            Close();
        }

        private void buttonMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        
        private void SetToolTip()
        {
            ToolTip ttip = new ToolTip();
            ttip.IsBalloon = true;
            ttip.SetToolTip(buttonExit, "Exit");
            ttip.SetToolTip(buttonMinimize, "MiniMize");
            
        }

        public void Confirm()
        {
            FlashMessageBox f = new FlashMessageBox();
            f.Location = new Point(Left + 100, Top + 290);
            
            f.ShowDialog();                                        
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (net.Connection == StateConnection.Connected && textBoxMessage.Text != "")
            {
                listBoxMessage.Items.Add("Toi: " + textBoxMessage.Text.Trim() + "");
                net.sendPacket("@: " + textBoxMessage.Text.Trim());
                textBoxMessage.Text = "";
            }
            
        }
        private delegate void AddChatMessageDelegate(string message);
        public void AddChatMessage(string message)
        {
            if (InvokeRequired)
            {
                Invoke(new AddChatMessageDelegate(AddChatMessage), new object[] { message });
                return;
            }
            listBoxMessage.Items.Add(message + "");
        }

        private void frmMain_Load_1(object sender, EventArgs e)
        {

        }

        private void btnCachchoi_Click(object sender, EventArgs e)
        {
            huongdan frm = new huongdan();
            frm.ShowDialog();
        }

        private void btnhelp_Click(object sender, EventArgs e)
        {

            try
            {
                System.Diagnostics.Process a = new System.Diagnostics.Process();
                a.StartInfo.FileName = @"co_tuong_nhap_mon.chm";
                a.StartInfo.Verb = "Open";
                //			a.StartInfo.Arguments=@" "+txtDiaChiHD.Text+" ";
                a.Start();
            }
            catch
            {
                MessageBox.Show("Files help không có ở địa chỉ này!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonShowPanel_Click(object sender, EventArgs e)
        {
            if (buttonShowPanel.Text == ">>>")
            {
                panelRight.Hide();
                MaximumSize = MinimumSize = Size = curSize = new Size(585, 615);
                buttonShowPanel.Text = "<<<";
            }
            else
            {
                panelRight.Show();
                MaximumSize = MinimumSize = Size = curSize = new Size(770, 615);
                buttonShowPanel.Text = ">>>";
            }
        }

       
    }
}
